﻿package 
{
	import flash.display.Sprite;  
	import flash.display.MovieClip;  
	import flash.net.URLLoader;  
	import flash.net.URLRequest;  
	import flash.display.Loader;  
	import flash.events.Event;  
	import flash.filters.BitmapFilter;  
	import flash.events.MouseEvent;  
	import fl.transitions.Tween;  
	import fl.transitions.easing.*;  
	import fl.transitions.TweenEvent;  
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.net.*;

	
	public class Link extends MovieClip
	{
		var linkname:String;
		var imagelocation:String;
		var address:String;
		var alphaTween:Tween;
		var screen = new Sprite();
		var targetx:int;
		var targetw:int;
		var targety:int;
			
		public function Link(l:String, i:String, a:String):void
		{			
			linkname = l;
			imagelocation = i;
			address = a;
			
		
			screen.graphics.beginFill(0x333333, 0);
			screen.graphics.drawRect(0,0,100,100);
			screen.graphics.endFill();
			
			addChild(screen);
			
			var loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
				
			var myrequest = new URLRequest(String(imagelocation));
			loader.load(myrequest);				
		}
		public function loaded(e:Event):void
		{
			    var templateBitmap:Bitmap = e.target.content;
   				var templateBitmapData:BitmapData = templateBitmap.bitmapData;
				var linkBitmap:Bitmap = new Bitmap(templateBitmapData);
				
				
				if (linkBitmap.width > 100 || linkBitmap.height > 100) {
					
					if (linkBitmap.width >= linkBitmap.height){
						var scale:Number = 100 / linkBitmap.width;
						linkBitmap.scaleX = scale;
						linkBitmap.scaleY = scale;
						trace(scale);
						
					}
					if (linkBitmap.height > linkBitmap.width) {
						var scale:Number = 100 / linkBitmap.height;
						linkBitmap.scaleX = scale;
						linkBitmap.scaleY =  scale;
						trace(scale);
						
					}
				}
				
			
				addChild(linkBitmap);
				targetx = (100 - linkBitmap.width) /2;
				targetw = linkBitmap.width;
				targety = linkBitmap.height;
				linkBitmap.x = targetx;
				linkBitmap.alpha = 0;
				alphaTween = new Tween(linkBitmap,"alpha", Regular.easeInOut, linkBitmap.alpha, 1, 1, true);
				//trace("name:  " + address + "     size:  " + this.width + "  " + this.height);
				this.addEventListener(MouseEvent.CLICK, clicked);			
				
		}
		public function clicked(e:Event):void
		{
			var link:URLRequest = new URLRequest(this.address);
			navigateToURL(link);
		}
	}
}